//
//  AppController.m
//
//  Created by Mechalogical on 19/01/2010.
//  Copyright 2010 Mechalogical Labs.
//
//  This file is part of BattleGrowl.
//  
//  BattleGrowl is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  BattleGrowl is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with BattleGrowl.  If not, see <http://www.gnu.org/licenses/>.
// 

#import "AppController.h"
#import "WowCharacter.h"


@implementation AppController

@synthesize preferencesWindow;

- (void) awakeFromNib{
	
	//Create the NSStatusBar and set its length
	statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
	
	//Used to detect where our files are
	NSBundle *bundle = [NSBundle mainBundle];
	
	//Allocates and loads the images into the application which will be used for our NSStatusItem
	statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"BattleGrowlStatusIcon" ofType:@"png"]];
	statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"BattleGrowlStatusIcon" ofType:@"png"]];
	
	//Sets the images in our NSStatusItem
	[statusItem setImage:statusImage];
	[statusItem setAlternateImage:statusHighlightImage];
	
	//Tells the NSStatusItem what menu to load
	[statusItem setMenu:statusMenu];
	//Sets the tooptip for our item
	[statusItem setToolTip:@"Battle Growl"];
	//Enables highlighting
	[statusItem setHighlightMode:YES];
	
	// load region Input
	[regionInput removeAllItems];
	[regionInput addItemWithTitle:@"EU"];
	[regionInput addItemWithTitle:@"US"];
	[regionInput addItemWithTitle:@"KR"];
	[regionInput addItemWithTitle:@"CN"];
	[regionInput addItemWithTitle:@"TW"];
	
		
	// setup default settings
	NSString *userDefaultsValuesPath;
    NSDictionary *userDefaultsValuesDict;
		// load the default values for the user defaults
    userDefaultsValuesPath=[[NSBundle mainBundle] pathForResource:@"UserDefaults" ofType:@"plist"];
    userDefaultsValuesDict=[NSDictionary dictionaryWithContentsOfFile:userDefaultsValuesPath];	
		// set them in the standard user defaults
    [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
	
	//start feed manager
	feedManager = [[FeedManager alloc] init];

}



-(IBAction)launchPreferencesWindow:(id)sender
{
	//Create the preferences window
	[preferencesWindow makeKeyAndOrderFront:sender];
	[NSApp activateIgnoringOtherApps:YES];
}


-(IBAction)launchAddCharacterPanel:(id)sender
{
	[NSApp beginSheet:addCharacterPanel modalForWindow:preferencesWindow
        modalDelegate:self didEndSelector:NULL contextInfo:nil];
}

-(IBAction)openProductWebsite:(id)sender
{
	[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.mechalogical.org/battlegrowl"]];
}

-(IBAction)addCharacter:(id)sender
{
	NSLog(@"adding character");
		
	[addCharacterPanel orderOut:nil];
    [NSApp endSheet:addCharacterPanel];
	
	// Add Character
	WowCharacter *character = [NSEntityDescription insertNewObjectForEntityForName:@"WowCharacter" inManagedObjectContext:[[NSApp delegate] managedObjectContext]];
	[character setName:[nameInput stringValue]];
	[character setRealm:[realmInput stringValue]];
	[character setRegion:[regionInput titleOfSelectedItem]];
	[character populateInferredFields];
	
	[[NSApp delegate] saveAction:self];
	
	
	// clear add character panel
	[nameInput setStringValue:@""];
	[realmInput setStringValue:@""];
	
	//sync feeds
	[feedManager syncFeedSubscriptions];	
}

-(IBAction)removeCharacter:(id)sender
{
	//sync feeds
	[feedManager syncFeedSubscriptions];	
}

-(IBAction)cancelAddCharacter:(id)sender
{
	NSLog(@"cancel add character");
	
	[addCharacterPanel orderOut:nil];
    [NSApp endSheet:addCharacterPanel];
}

- (NSString *)bundleVersion 
{
	return [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];
}

- (void) dealloc 
{
	//Releases the 2 images we loaded into memory
	[statusImage release];
	[statusHighlightImage release];
	[super dealloc];
}


@end
